package com.nsoft.pdfrender;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

public class Utils {
	public static final String EMPTY="";
	public static final String LINEBREAK="\r\n";
	public static final String IMAGE_PREFIX="image_";
	public static final String[] IMAGE_EXTENSIONS=new String[] {"jpg","png","gif","bmp","jp2"};
	
	public static final String DEF_AUTHOR="romantique";
	public static final String DEF_LANGUAGE="kr";

	public static String createXhtmlNav(String xhtmlName,String xhtmlFileName){
		StringBuffer buff=new StringBuffer();
		buff.append("<?xml version='1.0' encoding='utf-8' ?>"+LINEBREAK+
					"<!DOCTYPE html>"+LINEBREAK+
					"<html xmlns='http://www.w3.org/1999/xhtml' xmlns:epub='http://www.idpf.org/2007/ops' xml:lang='kr' lang='kr'>"+LINEBREAK+
					"<head>"+LINEBREAK+
					"	<meta http-equiv='default-style' content='text/html; charset=utf-8'/>"+LINEBREAK+
					"	<title>ToC</title>"+LINEBREAK+
					"</head>"+LINEBREAK+
					"<body>"+LINEBREAK+
					"<nav epub:type='toc'>"+LINEBREAK+
					"	<h2>Contents</h2>"+LINEBREAK+
					"	<ol>"+LINEBREAK);
		//buff.append("		<li><a href='cover.jpg'>cover</a></li>");
		buff.append("		<li><a href='"+xhtmlFileName+"'>begin</a></li>");
		buff.append("	</ol>"+LINEBREAK+
					"</nav>"+LINEBREAK+
					"</body>"+LINEBREAK+
					"</html>");
		return buff.toString();
	}
	/**
	 * read a binary file and return as a byte array
	 * 
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static byte[] readBinaryFile(String file) throws IOException {
		byte[] buff=new byte[1024];
		ByteArrayOutputStream baos=null;
		BufferedInputStream bis=null;
		try{
			baos=new ByteArrayOutputStream();
			bis=new BufferedInputStream(new FileInputStream(file));
			int read=0;
			while ((read=bis.read(buff))!=-1){
				baos.write(buff,0,read);
			}
			return baos.toByteArray();
		}catch(IOException e){
			throw e;
		}finally{
			if (bis!=null) bis.close();
			if (baos!=null) baos.close();
		}
	}
	
	/**
	 * replace all the non-word characters by its corresponding
	 * html code. just to make sure the content is compatible well
	 * with xhtml.
	 *  
	 * @param text
	 * @return
	 */
	public static String cleanToXhmtl(String text){
		return text.replaceAll("&","&amp;").
				replaceAll("<","&lt;").
				replaceAll(">","&gt;").
				replaceAll("&#8231;","‧").
				replaceAll(Utils.LINEBREAK,Utils.EMPTY);
	}

	public static String getPageHeader(String title){
		return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+Utils.LINEBREAK+
			"<!DOCTYPE html>"+Utils.LINEBREAK+
			"<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xml:lang=\"kr\">"+Utils.LINEBREAK+
			"<head>"+Utils.LINEBREAK+
			"	<title>"+title+"</title>"+Utils.LINEBREAK+
			"</head>"+Utils.LINEBREAK+
			"<body>"+Utils.LINEBREAK+
			"<div style=\"position:relative;text-align:justify;font-size:15pt;text-indent:10px;line-height:38px;\">"+Utils.LINEBREAK;
	}
	
	public static String getPageFooter(){
		return "</div>"+Utils.LINEBREAK+ 
			"</body>"+Utils.LINEBREAK+
			"</html>";
	}
	
	/**
	 * read a text file in EUC-KR encoding format
	 * and return a <b>StringBuffer</b>
	 * @param txtFile
	 * @return
	 * @throws IOException
	 */
	public static StringBuffer readFile(File txtFile) throws IOException {
		StringBuffer buff = new StringBuffer();
		BufferedReader br = new BufferedReader(new InputStreamReader(
				new FileInputStream(txtFile), Charset.forName("EUC-KR")));
		String line;
		while ((line = br.readLine()) != null) {
			buff.append(line + LINEBREAK);
		}
		br.close();
		return buff;
	}
	
	public static String readFile(String filePath) throws IOException {
		StringBuffer buff = new StringBuffer();
		BufferedReader br = new BufferedReader(new InputStreamReader(
				new FileInputStream(filePath), Charset.forName("EUC-KR")));
		String line;
		while ((line = br.readLine()) != null) {
			buff.append(line + LINEBREAK);
		}
		br.close();
		return buff.toString();
	}

	/**
	 * save a content to UTF-8 text file
	 * 
	 * @param filePath
	 * @param content
	 * @throws IOException
	 */
	public static void saveToFile(String filePath, String content)
			throws IOException {
		BufferedWriter fr = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(filePath), "UTF8"));
		fr.write(content);
		fr.close();
	}
	
	/**
	 * delete file/directory (delete all files inside)
	 * @param file
	 * @throws IOException
	 */
	public static void delete(File file) throws IOException{
	 	if(file.isDirectory()){
	 		if(file.list().length==0){
	 			file.delete();
			}else{
				String files[]=file.list();
				for (String temp:files){
					File fileDelete=new File(file,temp);
					delete(fileDelete);
				}
	
				if(file.list().length==0){
					file.delete();
				}
			}
	 	}else{
			file.delete();
		}
	}
}
